using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms; 
using System.Text.RegularExpressions;

namespace Administrator
{
    class DigitTextBox : TextBox
    {
        const int ES_NUMBER = 0x2000; 
        const int WM_PASTE = 0x0302;
        const int WM_KEYDOWN = 0x0100;

        const string NumberTemplate = @"^\d+$";

        

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parameters = base.CreateParams;
                parameters.Style |= ES_NUMBER;

                return parameters;
            }
        }

        protected override void WndProc(ref Message m)
        {

            try
            {
                if (m.Msg == WM_PASTE)
                {
                    string data = Clipboard.GetDataObject().GetData(DataFormats.Text) as string;
                    if (!Regex.IsMatch(data, NumberTemplate))
                        return;
                    else
                        if ((Convert.ToInt32(data) > 99999999) || (Convert.ToInt32(data) < 0))
                            return;
                }
                base.WndProc(ref m);
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
